package com.site.controller;

import com.google.common.collect.Maps;
import com.site.common.R;
import com.site.common.annotation.Log;
import com.wf.captcha.SpecCaptcha;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;

/**
 * 验证码测试
 */
@RestController
public class CaptchaTestController extends BaseController {

    /**
     * 返回验证码
     * @throws Exception
     */
    @Log(module = "captcha",action = "请求验证码")
    @GetMapping("/captcha")
    public R captcha() throws Exception {
        HttpServletRequest request = getRequest();
        HttpServletResponse response = getResponse();
        // 三个参数分别为宽、高、位数, 动态彩色验证码
        /*GifCaptcha gifCaptcha = new GifCaptcha(100, 48, 4);
        // 设置类型：字母数字混合
        gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
        //获取验证码
        String text = gifCaptcha.text();
        gifCaptcha.toBase64();*/
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text();// 获取验证码的字符
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        //redisUtil.setEx(key, verCode, 30, TimeUnit.MINUTES);
        // 将key和base64返回给前端
        Map<String, String> map = Maps.newHashMap();
        map.put("verCode",specCaptcha.toBase64());
        return R.ok(map);
    }

    /**
     * 验证码验证
     * @param username
     * @param password
     * @param verCode
     * @return
     */
    @PostMapping("/login")
    public R<String> login(String username, String password, String verCode){
        // 获取redis中的验证码
        //String redisCode = redisUtil.get(verKey);
        // 判断验证码
        //if (verCode==null || !redisCode.equals(verCode.trim().toLowerCase())) {
        //    return JsonResult.error("验证码不正确");
        //}
        return R.ok();
    }
}
